<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Artikel_model extends MY_Model {

    const TYPE_BERITA = "B";
    const TYPE_KABAR_RELAWAN = "KR";
    const TYPE_PUBLIK = "PJ";

    function __construct() {
        parent::__construct("article", "id", Array(
            "content_id" => "Content_model"
        ));
        $this->delAttribute = "is_deleted";
    }

    protected function read($where = Array(), $fetchForeign = false, $depth = 1, $page = -1, $limit = 10) {
        $results = parent::read($where, $fetchForeign, $depth, $page, $limit);

        foreach ($results as $result) {
            if (is_object($result->content_id)) {
                switch ($result->type) {
                    case Artikel_model::TYPE_BERITA:
                        $result->content_id->short = substr(preg_replace("/(<([^>]*)>)/i", "", $result->content_id->text), 0, 640) . " <a href='" . site_url("artikel/berita/" . $result->id) . "'>Lebih lanjut...</a>";
                        $result->content_id->shortHL = substr(preg_replace("/(<([^>]*)>)/i", "", $result->content_id->text), 0, 950) . " <a href='" . site_url("artikel/berita/" . $result->id) . "'>Lebih lanjut...</a>";
                        $hlPicSrc = res_url("img/5pilar_bg.png");
                        if (count($result->content_id->pics) > 0) {
                            $hlPicSrc = $result->content_id->pics[0]->src;
                        }
                        $result->content_id->hlPicSrc = $hlPicSrc;
                        break;
                    case Artikel_model::TYPE_KABAR_RELAWAN:
                        $result->content_id->short = substr(preg_replace("/(<([^>]*)>)/i", "", $result->content_id->text), 0, 750) . " <a href='" . site_url("artikel/kr/" . $result->id) . "'>Lebih lanjut...</a>";
                        $result->content_id->shortHL = substr(preg_replace("/(<([^>]*)>)/i", "", $result->content_id->text), 0, 390) . " <a href='" . site_url("artikel/kr/" . $result->id) . "'>Lebih lanjut...</a>";
                        $hlPicSrc = res_url("img/logo_ps.png");
                        if (count($result->content_id->pics) > 0) {
                            $hlPicSrc = $result->content_id->pics[0]->src;
                        }
                        $result->content_id->hlPicSrc = $hlPicSrc;
                        break;
                    case Artikel_model::TYPE_PUBLIK:
                        $result->content_id->short = substr(preg_replace("/(<([^>]*)>)/i", "", $result->content_id->text), 0, 750) . " <a href='" . site_url("artikel/pj/" . $result->id) . "'>Lebih lanjut...</a>";
                        $result->content_id->shortHL = substr(preg_replace("/(<([^>]*)>)/i", "", $result->content_id->text), 0, 390) . " <a href='" . site_url("artikel/pj/" . $result->id) . "'>Lebih lanjut...</a>";
                        $hlPicSrc = res_url("img/logo_ps.png");
                        if (count($result->content_id->pics) > 0) {
                            $hlPicSrc = $result->content_id->pics[0]->src;
                        }
                        $result->content_id->hlPicSrc = $hlPicSrc;
                        break;
                }
            }
        }

        return $results;
    }

    function createBerita($title, $text, $sumber, $tags = Array(), $pics = Array()) {
        $this->load->model("User_model");
        $curUserId = $this->curUserId();
        $roles = $this->User_model->fetchRoles($curUserId);
        $isAdmin = in_array("berita_auditor", $roles);
        $text = preg_replace("/<p>/", "<p>" . $sumber . " -- ", $text, 1);
        $contentId = $this->Content_model->create($title, $text, NULL, NULL, false, $tags, $pics);
        return parent::create(Array(
                    "content_id" => $contentId,
                    "type" => Artikel_model::TYPE_BERITA,
                    "is_approved" => $isAdmin
        ));
    }

    function createKR($title, $text, $tags = Array(), $pics = Array()) {
        $this->load->model("User_model");
        $curUserId = $this->curUserId();
        $roles = $this->User_model->fetchRoles($curUserId);
        $isAdmin = in_array("kr_auditor", $roles);
        $contentId = $this->Content_model->create($title, $text, NULL, NULL, false, $tags, $pics);
        return parent::create(Array(
                    "content_id" => $contentId,
                    "type" => Artikel_model::TYPE_KABAR_RELAWAN,
                    "is_approved" => $isAdmin
        ));
    }

    function createPJ($title, $text, $tags = Array(), $pics = Array()) {
        $this->load->model("User_model");
        $curUserId = $this->curUserId();
        $roles = $this->User_model->fetchRoles($curUserId);
        $isAdmin = in_array("pj_auditor", $roles);
        $contentId = $this->Content_model->create($title, $text, NULL, NULL, false, $tags, $pics);
        return parent::create(Array(
                    "content_id" => $contentId,
                    "type" => Artikel_model::TYPE_PUBLIK,
                    "is_approved" => $isAdmin
        ));
    }

    function beritaByPoster($idPoster, $fetchForeign = false, $depth = 1) {
        return $this->searchByPoster($idPoster, Artikel_model::TYPE_BERITA, $fetchForeign, $depth);
    }

    function krByPoster($idPoster, $fetchForeign = false, $depth = 1) {
        return $this->searchByPoster($idPoster, Artikel_model::TYPE_KABAR_RELAWAN, $fetchForeign, $depth);
    }

    function pjByPoster($idPoster, $fetchForeign = false, $depth = 1) {
        return $this->searchByPoster($idPoster, Artikel_model::TYPE_PUBLIK, $fetchForeign, $depth);
    }

    function beritaById($id, $fetchForeign = false, $depth = 1) {
        $berita = $this->findById($id, $fetchForeign, $depth);
        if ($berita != NULL) {
            if ($berita->type == Artikel_model::TYPE_BERITA) {
                return $berita;
            }
        }
        return NULL;
    }

    function krById($id, $fetchForeign = false, $depth = 1) {
        $berita = $this->findById($id, $fetchForeign, $depth);
        if ($berita != NULL) {
            if ($berita->type == Artikel_model::TYPE_KABAR_RELAWAN) {
                return $berita;
            }
        }
        return NULL;
    }

    function pjById($id, $fetchForeign = false, $depth = 1) {
        $berita = $this->findById($id, $fetchForeign, $depth);
        if ($berita != NULL) {
            if ($berita->type == Artikel_model::TYPE_PUBLIK) {
                return $berita;
            }
        }
        return NULL;
    }

    function beritaAll($where = Array(), $fetchForeign = false, $depth = 1, $page = -1, $limit = 10) {
        $where["type"] = Artikel_model::TYPE_BERITA;
        return parent::findAll($where, $fetchForeign, $depth, $page, $limit);
    }

    function krAll($where = Array(), $fetchForeign = false, $depth = 1, $page = -1, $limit = 10) {
        $where["type"] = Artikel_model::TYPE_KABAR_RELAWAN;
        return parent::findAll($where, $fetchForeign, $depth, $page, $limit);
    }

    function pjAll($where = Array(), $fetchForeign = false, $depth = 1, $page = -1, $limit = 10) {
        $where["type"] = Artikel_model::TYPE_PUBLIK;
        return parent::findAll($where, $fetchForeign, $depth, $page, $limit);
    }

    private function searchByPoster($idPoster, $type, $fetchForeign = false, $depth = 1) {
        $query = $this->db->query("SELECT article.id FROM article, content WHERE type = '" . $type . "' AND article.content_id = content.id AND article.is_deleted = 0 AND content.poster_id = " . $idPoster . " ORDER BY article.is_approved ASC, content.posted DESC LIMIT 0, 10");
        $resArr = Array();

        foreach ($query->result() as $res) {
            array_push($resArr, $this->findById($res->id, $fetchForeign, $depth));
        }

        return $resArr;
    }

    public function beritaHL() {
        $hlList = $this->fetchHL(Artikel_model::TYPE_BERITA, 1);
        if (count($hlList) > 0) {
            return $hlList[0];
        } else {
            return NULL;
        }
    }

    public function krHL() {
        return $this->fetchHL(Artikel_model::TYPE_KABAR_RELAWAN, 2);
    }

    public function pjHL() {
        return $this->fetchHL(Artikel_model::TYPE_PUBLIK, 2);
    }

    private function fetchHL($type, $limit = 5) {
        $this->db->select("hl_article.article_id as id")->from("hl_article, article")->where("article.type = '" . $type . "' AND hl_article.article_id = article.id AND article.is_approved = 1 AND article.is_deleted = 0")->order_by("hl_article.timestamp", "DESC")->limit($limit, 0);
        $query = $this->db->get();
        $artList = Array();
        foreach ($query->result() as $row) {
            $article = $this->findById($row->id, true, 2);
            if ($article != NULL) {
                array_push($artList, $article);
            }
        }
        return $artList;
    }

    public function makeHL($id) {
        $artikel = $this->Artikel_model->findById($id);
        if ($artikel != NULL) {
            if ($artikel->is_approved) {
                $this->db->from("hl_article")->where("article_id", $artikel->id);
                $query = $this->db->get();
                $hl = $query->row();
                if ($hl != NULL) {
                    echo $this->db->update("hl_article", Array("timestamp" => date("Y-m-d H:i:s")), Array("id" => $hl->id));
                } else {
                    echo $this->db->insert("hl_article", Array("timestamp" => date("Y-m-d H:i:s"), "article_id" => $artikel->id));
                }
            }
        }

        return 0;
    }

    private function fetchRecentPics($type, $page = 0, $limit = 5) {
        $this->db->select("content_pics.image_id as id");
        $this->db->distinct();
        $this->db->from("article, content, content_pics");
        $this->db->where("article.content_id = content.id");
        $this->db->where("content.id = content_pics.content_id");
        $this->db->where("article.is_approved", true);
        $this->db->where("article.is_deleted", false);
        $this->db->where("article.type", $type);
        $this->db->order_by("content_pics.image_id", "desc");
        $this->db->limit($limit, ($page * $limit));

        $query = $this->db->get();
        $resArr = Array();

        $this->load->model("Image_model");
        foreach ($query->result() as $res) {
            array_push($resArr, $this->Image_model->findById($res->id));
        }

        return $resArr;
    }

    function beritaRecentPics($page = 0, $limit = 5) {
        return $this->fetchRecentPics(Artikel_model::TYPE_BERITA, $page, $limit);
    }

    function krRecentPics($page = 0, $limit = 5) {
        return $this->fetchRecentPics(Artikel_model::TYPE_KABAR_RELAWAN, $page, $limit);
    }

    function pjRecentPics($page = 0, $limit = 5) {
        return $this->fetchRecentPics(Artikel_model::TYPE_PUBLIK, $page, $limit);
    }

    private function fetchRecent($type, $page = 0, $limit = 5, $excludes = "") {
        $query = $this->db->query("SELECT article.id FROM article, content WHERE article.type = '" . $type . "'" . (!($excludes == "") ? " AND article.id NOT IN (" . $excludes . ")" : "") . " AND article.content_id = content.id AND article.is_deleted = 0 AND article.is_approved = 1 ORDER BY content.posted DESC LIMIT " . ($page * $limit) . ", " . $limit);
        $resArr = Array();

        foreach ($query->result() as $res) {
            array_push($resArr, $this->findById($res->id, true, 3));
        }

        return $resArr;
    }

    public function beritaRecent($page = 0, $limit = 4) {
        $hl = $this->beritaHL();
        $result = Array();
        $excludes = "";
        if ($hl != NULL) {
            $excludes .= $hl->id;
        }
        foreach ($this->fetchRecent(Artikel_model::TYPE_BERITA, $page, $limit, $excludes) as $berita) {
            array_push($result, $berita);
        }
        return $result;
    }

    public function krRecent($page = 0, $limit = 5) {
        $excludes = "";
        $i = 0;
        foreach ($this->krHL() as $kr) {
            $excludes .= ($i > 0 ? "," : "") . $kr->id;
            $i++;
        }
        return $this->fetchRecent(Artikel_model::TYPE_KABAR_RELAWAN, $page, $limit, $excludes);
    }

    public function pjRecent($page = 0, $limit = 5) {
        $excludes = "";
        $i = 0;
        foreach ($this->pjHL() as $pj) {
            $excludes .= ($i > 0 ? "," : "") . $pj->id;
            $i++;
        }
        return $this->fetchRecent(Artikel_model::TYPE_PUBLIK, $page, $limit, $excludes);
    }

}